package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.Passport;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.Battery;
import cc.yugu.yhdw.common.domain.hdw.Cabinet;
import cc.yugu.yhdw.common.domain.hdw.CabinetBox;
import cc.yugu.yhdw.common.domain.hdw.ExchangeOrder;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.common.protocol.msg05.Msg052000003;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.biz.client.ClientBizUtils;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.basic.PassportService;
import cc.yugu.yhdw.webserver.service.hdw.CabinetBoxService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangeOrderService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/security/hdw/exchange_order")
public class ExchangeOrderController extends SecurityController {

    @Autowired
    ExchangeOrderService exchangeOrderService;
    @Autowired
    CabinetBoxService cabinetBoxService;
    // 新增客户service注入
    @Autowired
    PassportService passportService;
//
//    @ResponseBody
//    @ViewModel(ViewModel.JSON_ARRAY)
//    @RequestMapping(value = "monthTree.htm")
//    public void monthTree(HttpServletResponse response) throws Exception {
//        response.setContentType(ConstEnum.ContentType.JSON.getValue());
//        exchangeOrderService.monthTree(response.getOutputStream());
//    }

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute("statusList", ExchangeOrder.OrderStatus.values());
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_03.getValue());
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "view_map.htm")
    public String viewMap(Model model, String id) {
        model.addAttribute("id", id);
        return "/security/hdw/exchange_order/view_map";
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(ExchangeOrder search, HttpServletRequest request) {
        if (search.getSecondBatteryId() == null || "".equals(search.getSecondBatteryId().trim())) {
            search.setSecondBatteryId(null);
        }
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        } else if (getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
        return PageResult.successResult(exchangeOrderService.findPage(search));
    }

    @RequestMapping(value = "view.htm")
    public String view_image(Model model, String id) {
        ExchangeOrder entity = exchangeOrderService.find(id);
        // 新增修改，查询客户真实姓名
        Passport real = passportService.find(entity.getCustomerId());

        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
            // 新增
            model.addAttribute("real",real);
        }
        return "/security/hdw/exchange_order/view";
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "/open_full_box")
    public ExtResult openFullBox(String orderId) throws InterruptedException {
        ExchangeOrder exchangeOrder = exchangeOrderService.find(orderId);
        if (exchangeOrder == null) {
            return ExtResult.failResult("订单不存在");
        }
        if (exchangeOrder.getOrderStatus() != ExchangeOrder.OrderStatus.OLD_BATTERY.getValue() && exchangeOrder.getOrderStatus() != ExchangeOrder.OrderStatus.PAY.getValue()) {
            return ExtResult.failResult("订单状态异常,不能开满箱");
        }
        String boxNum = cabinetBoxService.findOneAvailableBoxNum(exchangeOrder.getCabinetId(), CabinetBox.BoxStatus.FULL.getValue());
        if (StringUtils.isEmpty(boxNum)) {
            return ExtResult.failResult("没有可用电池");
        }
        ExtResult restResult =  ClientBizUtils.noticeOpenBox(appConfig,  exchangeOrder.getCabinetId(), boxNum);
        if (restResult.isSuccess()) {
            int effect = exchangeOrderService.complete(exchangeOrder.getId(), exchangeOrder.getCabinetId(), boxNum);
            if (effect > 0) {
                return ExtResult.successResult("开箱成功");
            } else {
                return ExtResult.failResult("订单状态错误");
            }

        } else {
            return restResult;
        }
    }
    
    /**
     * 新增 - 电池管理重定向到换电订单业务上的数据
     * @Param [model]
     * @Return void
     * @Author zxj 2018/8/29
     */
    @RequestMapping(value = "/second_battery_id.htm")
    public String secondBatteryId(Model model, String secondBatteryId) {
        model.addAttribute("secondBatteryId", secondBatteryId);
        model.addAttribute("statusList", ExchangeOrder.OrderStatus.values());
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_03.getValue());
        return "/security/hdw/exchange_order/index";
    }
    
}